package com.zm.demo.core.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeMap;

class LRUCache {

    Map<Integer,Integer> cache = new HashMap<>();

    List<Integer> keys = new ArrayList<>();
    int capacity;

    public LRUCache(int capacity) {
        this.capacity = capacity;
    }
    
    public int get(int key) {
        Integer value = cache.get(key);
        if (value!= null){
            if (keys.size()>1){
                keys.remove(Integer.valueOf(key));
                keys.add(key);
            }
            return value;
        }else {
            return -1;
        }
    }
    
    public void put(int key, int value) {
        if (cache.get(key) != null){
            keys.remove(Integer.valueOf(key));
        }else if(keys.size() == capacity){
            cache.remove(keys.get(0));
            keys.remove(0);
        }
        cache.put(key,value);
        keys.add(key);

    }
}

class LRUCachePO{
    int val;
    long timestamp;

    LRUCachePO(int val, long timestamp){
        this.val = val;
        this.timestamp = timestamp;
    }
}